package com.iespring.service.app.userregister;

import com.iespring.bean.admin.user.entity.MallUser;
import com.iespring.bean.app.user.bo.UserRegisterBo;
import com.iespring.bean.app.user.bo.WxResetBo;
import com.iespring.bean.app.user.entity.UserInfo;
import com.iespring.bean.app.user.vo.WxLoginVo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.mapper.admin.MallUserMapper;
import com.iespring.util.Md5Utils;
import com.iespring.util.SMSUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * Auth: 杨宇健
 * Time: 2023/1/2 15:59
 * Description: 用户注册 删除service
 **/
@Service
public class WxUserRegisterServiceImpl implements WxUserRegisterService {
    @Resource
    MallUserMapper userMapper;

    @Override
    public BaseRespVo register(UserRegisterBo user) throws Exception {
        boolean validation = SMSUtils.validation(user.getCode(), user.getMobile());
        if (!validation) {
            return BaseRespVo.error("验证码不正确", 500);
        }

        //转换为加密密码
        String md5Password = Md5Utils.getMd5(user.getPassword());
        user.setPassword(md5Password);

        MallUser mallUserPo = new MallUser();
        mallUserPo.setMobile(user.getMobile());
        //BeanUtils.copyProperties(user, mallUserPo);
        List<MallUser> mallUserList = userMapper.selectUserByMobile(mallUserPo.getMobile(), false);
        //已用该手机注册过账号，不允许再注册
        if (mallUserList.size() > 0) {
            return BaseRespVo.error("请不要用一个手机注册多个账号", 500);
        }
        BeanUtils.copyProperties(user, mallUserPo);
        Date now = new Date();
        mallUserPo.setAddTime(now);
        mallUserPo.setUpdateTime(now);
        mallUserPo.setDeleted(false);

        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        mallUserPo.setLastLoginIp(request.getRemoteHost());

        mallUserPo.setWeixinOpenid(user.getWxCode());
        mallUserPo.setWeixinOpenid("");
        mallUserPo.setStatus((byte) 0);
        mallUserPo.setSessionKey("");
        mallUserPo.setAvatar("storageImg/47533a3bf57f480bafc62fffde63d710.jpg");

        mallUserPo.setGender((byte) 0);
        mallUserPo.setNickname(user.getUsername());

        int affect = userMapper.insert(mallUserPo);

        Subject subject = SecurityUtils.getSubject();
        Serializable sessionId = subject.getSession().getId();

        if (affect == 1) {

            WxLoginVo wxLoginVo = new WxLoginVo();
            wxLoginVo.setToken(String.valueOf(sessionId));
            UserInfo userInfo = new UserInfo();
            userInfo.setAvatarUrl(mallUserPo.getAvatar());
            userInfo.setNickName(mallUserPo.getNickname());
            wxLoginVo.setUserInfo(userInfo);
            return BaseRespVo.ok(wxLoginVo);
        }
        throw new RuntimeException("插入账号 " + user.getMobile() + " 失败");

    }

    @Override
    public BaseRespVo reset(WxResetBo reset) throws Exception {
        boolean validation = SMSUtils.validation(reset.getCode(), reset.getMobile());
        if (!validation) {
            return BaseRespVo.error("验证码错误", 400);
        }
        List<MallUser> mallUsers = userMapper.selectUserByMobile(reset.getMobile(), false);
        if (mallUsers.size() == 0) {
            return BaseRespVo.error("该账号不存在或已注销", 400);
        }
        MallUser mallUser = mallUsers.get(0);
        mallUser.setPassword(Md5Utils.getMd5(reset.getPassword()));
        mallUser.setUpdateTime(new Date());
        userMapper.updateByPrimaryKey(mallUser);
        return BaseRespVo.ok();
    }
}
